package com.fosu.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fosu.domain.Campus;
import com.fosu.domain.Label;

import com.fosu.domain.ProductCategories;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Desription
 * 精简商品信息类(首页用)
 * @author manno
 * @date 2020/10/22
 */
@Data
@Component
public class MiniProduct
{
    public MiniProduct()
    {
        this.productCategories = new ArrayList<>();
        this.categoriesString = new StringBuffer();
        this.foldedImgNumber = 0;
    }

    /**
     * 商品id
     */
    private Integer productId;
    /**
     * 商品所属用户id
     */
    private Integer userId;
    private String nickName;
    private String avatarUrl;
    /**
     * 商品图片组
     */
    private List<String> productImgs;
    /**
     * 商品缩略图
     * */
    private List<String> compressImgs;

    /**
     * 缩略图组的大小不大于4
     * @param compressImgs 商品图片组url集合
     */
    public void setCompressImgs(List<String> compressImgs)
    {
        this.compressImgs = compressImgs;
        if (this.compressImgs.size() > 4)
        {
            this.compressImgs = this.compressImgs.subList(0, 4);
        }
    }
    /**
     * 图片组的大小不大于4
     * @param productImgs 商品图片组url集合
     */
    public void setProductImgs(List<String> productImgs)
    {
        this.productImgs = productImgs;
        if (this.productImgs.size() > 4)
        {
            this.productImgs = this.productImgs.subList(0, 4);
            this.foldedImgNumber = productImgs.size() - 4;
        }
    }

    /**
     * 未显示的图片数量
     */
    private int foldedImgNumber;
    /**
     * 商品价格
     */
    private Double productPrice;
    /**
     * 商品标签组
     */
    private Label productLabels;

    //入手价
    private Double purchasePrice;

    /**
     * 商品分类组
     */
    @JsonIgnore
    private List<ProductCategories> productCategories;

    /**处理分类字串
     * @param productCategories 商品分类集合
     */
    public void setProductCategories(List<ProductCategories> productCategories)
    {
        this.productCategories = productCategories;
        if (this.productCategories.size() != 0)
        {
            for (ProductCategories p : this.productCategories)
            {
                categoriesString.append(p.getCategoriesName() + ",");
            }
        }
    }

    /**
     * 商品自定义分类(json化不进行序列化)
     */
    @JsonIgnore
    private String rawCustomizeCategories;

    /**
     * 自动处理自定义分类字串
     */
    public void setRawCustomizeCategories(String rawCustomizeCategories)
    {
        this.rawCustomizeCategories = rawCustomizeCategories;
        String[] strs = rawCustomizeCategories.split("[,]");
        for (int i = 0; i < strs.length; i++)
        {
            if (i != strs.length - 1)
            {
                categoriesString.append(strs[i] + ",");
            } else
            {
                categoriesString.append(strs[i]);
            }
        }

    }

    /**
     * 处理过的商品自定义分类
     */
    private StringBuffer categoriesString;
    /**
     * 商品标题
     */
    private String productTitle;
    /**
     * 修改日期
     */
    private Timestamp lastModifiedDate;
    /**
     * 浏览量
     */
    private Integer pageViews;
    /**
     * 商品所在校区
     */
    private List<Campus> productCampus;
    /**
     * 在查询时规范化时间戳格式
     * @return Timestamp 规范化后的时间戳对象
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Timestamp getLastModifiedDate()
    {
        return lastModifiedDate;
    }

    private Integer offShelf;
}
